package week07

import "strings"

// 557. 反转字符串中的单词 III
// https://leetcode-cn.com/problems/reverse-words-in-a-string-iii/

func reverseWords(s string) string {
	strs := strings.Split(s, " ")
	for i := range strs {
		strs[i] = reverse(strs[i])
	}
	return strings.Join(strs, " ")
}

func reverse(s string) string {
	buf := []byte(s)
	for i := 0; i < len(buf)/2; i++ {
		buf[i], buf[len(buf)-i-1] = buf[len(buf)-i-1], buf[i]
	}
	return string(buf)
}